package com.cr.sync.service.impl;

import com.cr.common.utils.StringUtils;
import com.cr.sync.domain.vo.AreaCountVo;
import com.cr.sync.domain.vo.CountVo;
import com.cr.sync.mapper.TbSyncMainBodyInfoMapper;
import com.cr.sync.mapper.TbSyncOrganInfoMapper;
import com.cr.sync.mapper.TbSyncSupervisePersonMapper;
import com.cr.sync.service.LsdCountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class LsdCountServiceImpl implements LsdCountService {

    @Resource
    private TbSyncMainBodyInfoMapper tbSyncMainBodyInfoMapper;

    @Resource
    private TbSyncOrganInfoMapper tbSyncOrganInfoMapper;

    @Resource
    private TbSyncSupervisePersonMapper tbSyncSupervisePersonMapper;

    private static final String[] AREA = new String[] {"回民区", "玉泉区", "新城区", "赛罕区", "土默特左旗", "托克托县", "清水河县", "武川县", "和林格尔县"};

    @Override
    public List<AreaCountVo> getAreaCount(String area) {
        // 主体数量
        List<CountVo> allEntityCounts = tbSyncMainBodyInfoMapper.selectAllCount(area);
        // 机构数量
        List<CountVo> allOrganCounts = tbSyncOrganInfoMapper.selectAllCount(area);
        // 监管人员数量
        List<CountVo> inspectionCounts = tbSyncSupervisePersonMapper.selectInspectionCount(area);
        List<CountVo> assistCounts = tbSyncSupervisePersonMapper.selectAssistCount(area);
        List<CountVo> informationCounts = tbSyncSupervisePersonMapper.selectInformationCount(area);

        List<AreaCountVo> areaCountVos = new ArrayList<>();
        for (String areaName : AREA) {
            AreaCountVo areaCountVo = new AreaCountVo();
            for (CountVo countVo : allEntityCounts) {
                if (areaName.equals(countVo.getLabel())) {
                    areaCountVo.setAllEntity(countVo.getValue());
                }
            }

            for (CountVo countVo : allOrganCounts) {
                if (areaName.equals(countVo.getLabel())) {
                    areaCountVo.setAllOrgan(countVo.getValue());
                }
            }

            for (CountVo countVo : inspectionCounts) {
                if (areaName.equals(countVo.getLabel())) {
                    areaCountVo.setInspection(countVo.getValue());
                }
            }

            for (CountVo countVo : assistCounts) {
                if (areaName.equals(countVo.getLabel())) {
                    areaCountVo.setAssist(countVo.getValue());
                }
            }

            for (CountVo countVo : informationCounts) {
                if (areaName.equals(countVo.getLabel())) {
                    areaCountVo.setInformation(countVo.getValue());
                }
            }
            areaCountVo.setArea(areaName);
            areaCountVos.add(areaCountVo);
        }

        if(StringUtils.isNotBlank(area)) {
            // 只返回指定区域数据
            return areaCountVos.stream().filter(areaCountVo -> areaCountVo.getArea().equals(area)).collect(Collectors.toList());
        }
        return areaCountVos;
    }
}
